<template>
  <div>
    <!-- 需求计划汇总查询N+2&N+3 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhN2monthlyPlan"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <BaseDialog :visible.sync="isDealer" :title="'基本信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import moment from 'moment';
import { getRegion, vehicleConfiguration } from '@/api/omFinance';
import { exportExcelFile } from '@/utils';
import { queryList, exportExcel ,exporturl} from '@/api/omSale/salesPlan/N2monthlyPlan';
import OwnerSearch from '../OwnerSearch';

export default {
  name: 'N2monthlyPlan',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {
        isDealer: 10041002,
        planStatus: 60241002,
        planYearAndMonth: moment().format('YYYY-MM')
      },
      formData: {},
      exportLoading: false,
      isDealer: false
    };
  },
  components: {
    OwnerSearch
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getDeptData();

    this.getvehicleList();
  },
  methods: {
    //加载事业部数据
    async getDeptData() {
      const res = await getRegion();
      if (res.resultCode === 200) {
        res.data.map(x => {
          this.topFilterList.find(item => item.fieldName == 'deptId').itemList.push({ text: x.orgName, value: x.id });
        });
      }
    },
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'brandProductList').itemList = res.data;
    },
    seacherFun() {
      this.isDealer = true;
    },
    emitCloseHandler(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCodes').initialValue = dealerCodes.toString();
      }
    },
    changeHandle(params) {
      this.columns.find(x => x.dataIndex == 'n1').title = `${moment(params.planYearAndMonth)
        .subtract(-1, 'months')
        .format('YYYY年MM月')}(N+1)`;
      this.columns.find(x => x.dataIndex == 'n2').title = `${moment(params.planYearAndMonth)
        .subtract(-2, 'months')
        .format('YYYY年MM月')}(N+2)`;
      this.columns.find(x => x.dataIndex == 'n3').title = `${moment(params.planYearAndMonth)
        .subtract(-3, 'months')
        .format('YYYY年MM月')}(N+3)`;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // 导出
    async exportHandle() {
 let json = Object.assign({}, { CODE: 'CODE_0000027' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }




      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '需求计划汇总查询（N+1）');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '事业部',
          fieldName: 'deptId',
          placeholder: '请选择',
          itemList: []
        },

        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCodes',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'brandProductList',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装'] },
          itemList: []
        },

        {
          type: 'DATE',
          label: '月份',
          fieldName: 'planYearAndMonth',
          initialValue: moment().format('YYYY-MM'),
          dateType: 'month',
          clearable: false
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        // {
        //   title: '产品名称',
        //   dataIndex: 'productName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '产品代码',
        //   dataIndex: 'productCode',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '品牌名称',
        //   dataIndex: 'brandName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装代码',
          dataIndex: 'optionCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'configName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        // {
        //   title: '内饰',
        //   dataIndex: 'trimColorName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '外饰',
        //   dataIndex: 'colorName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '指导价',
        //   dataIndex: 'oemDirectivePrice',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: `${moment()
            .subtract(-1, 'months')
            .format('YYYY年MM月')}(N+1)`,
          dataIndex: 'n1',
          filterType: 'input',
          align: 'center',
          children: [
            {
              title: '提报数量',
              dataIndex: 'quantity1',
              sorter: true,
              filter: true,
              filterType: 'input',
              summation: true
            }
          ]
        },
        {
          title: `${moment()
            .subtract(-2, 'months')
            .format('YYYY年MM月')}(N+2)`,
          dataIndex: 'n2',
          filterType: 'input',
          align: 'center',
          children: [
            {
              title: '提报数量',
              dataIndex: 'quantity2',
              sorter: true,
              filter: true,
              filterType: 'input',
              summation: true
            }
          ]
        },
        {
          title: `${moment()
            .subtract(-3, 'months')
            .format('YYYY年MM月')}(N+3)`,
          dataIndex: 'n3',
          filterType: 'input',
          align: 'center',
          children: [
            {
              title: '提报数量',
              dataIndex: 'quantity3',
              sorter: true,
              filter: true,
              filterType: 'input',
              summation: true
            }
          ]
        },
        {
          title: '合计',
          dataIndex: 'total',
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true
        }
      ];
    }
  }
};
</script>
